#!/usr/bin/env python
# -*- coding:utf-8 -*-

from persistentqueue import PersistentQueue

def testLargeCacheImpl(expected_size_at_start, name, ELEMENTS, CACHE_SIZE):
    p = PersistentQueue(name, CACHE_SIZE)
    print 'At startup: Queue length:', len(p)
    if len(p) != expected_size_at_start:
        print '\n\t\t\t*** EXPECTED %d but got %d' % (expected_size_at_start, len(p))
    print 'Enqueueing %d items, cache size = %d' % (ELEMENTS,
                                                    p.cache_size)
    for a in range(ELEMENTS):
        record = '%09d' % a
        p.push(record)
    p.sync()
    size_at_close = len(p)
    print 'At close: queue length:', size_at_close
    p.close()
    return size_at_close

def testLargeCache():
    name = 'cache_size_issue'
    ELEMENTS = 5
    CACHE_SIZE = ELEMENTS * 3
    p = PersistentQueue(name, CACHE_SIZE)
    expected = len(p)
    p.sync()
    p.close()
    expected = testLargeCacheImpl(expected, name, ELEMENTS, CACHE_SIZE)
    expected = testLargeCacheImpl(expected, name, ELEMENTS, CACHE_SIZE)
    testLargeCacheImpl(expected, name, ELEMENTS, CACHE_SIZE)

if __name__ == "__main__":
    testLargeCache()

